/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SubsitemaAgenda;

import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author mario
 */
public class DialogoBuscarPaciente extends javax.swing.JDialog {

    DialogoAgregarCita diag;
    public DialogoBuscarPaciente(java.awt.Frame parent, boolean modal, DialogoAgregarCita diag) {
        super(parent, modal);
        initComponents();
        this.setTitle("Buscar paciente");
        setIconImage(new ImageIcon(getClass().getResource("/GUI/Img/logo2.png")).getImage());        
        this.diag = diag;
        actualizarTabla();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        pacienteABuscar = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        contenedorPaciente = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Paciente");

        pacienteABuscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                pacienteABuscarKeyReleased(evt);
            }
        });

        contenedorPaciente.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nombre", "Rut"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        contenedorPaciente.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                contenedorPacienteMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(contenedorPaciente);
        contenedorPaciente.getColumnModel().getColumn(0).setResizable(false);
        contenedorPaciente.getColumnModel().getColumn(1).setMinWidth(90);
        contenedorPaciente.getColumnModel().getColumn(1).setPreferredWidth(90);
        contenedorPaciente.getColumnModel().getColumn(1).setMaxWidth(90);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pacienteABuscar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(pacienteABuscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void pacienteABuscarKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_pacienteABuscarKeyReleased
        
        
        if (!pacienteABuscar.getText().equals("")) {
            limpiarTabla();
            java.util.List<service.Paciente> lista = filtrarPacientes(pacienteABuscar.getText());
            filtrarTabla(lista);
        }
        else{
            limpiarTabla();
            actualizarTabla();
        }
    }//GEN-LAST:event_pacienteABuscarKeyReleased

    private void contenedorPacienteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_contenedorPacienteMouseClicked
        int fila = contenedorPaciente.rowAtPoint(evt.getPoint());
        if ((fila > -1) && evt.getClickCount() == 2) {
            diag.campoRutPaciente.getRut().setText((String)contenedorPaciente.getValueAt(fila, 1));
            diag.campoNombrePaciente.setText((String)contenedorPaciente.getValueAt(fila, 0));
            this.dispose();
        }
    }//GEN-LAST:event_contenedorPacienteMouseClicked

   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable contenedorPaciente;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField pacienteABuscar;
    // End of variables declaration//GEN-END:variables

    private void limpiarTabla() {
        DefaultTableModel temp = (DefaultTableModel) contenedorPaciente.getModel();
        for(int i = contenedorPaciente.getRowCount() -1; i >= 0; i--){
            temp.removeRow(i);
        } 
    }

    private void actualizarTabla() {
        this.limpiarTabla();
        java.util.List<service.Paciente> pacientes = listarPacientes();
        int i = 0;
        for(service.Paciente p : pacientes){
            if(p.isVisible()){
                this.agregarFila();                
                contenedorPaciente.setValueAt(p.getNombres()+ " " + p.getApellidoPaterno() + " " + p.getApellidoMaterno() , i, 0);                
                contenedorPaciente.setValueAt(p.getRut(), i, 1);                              
                i++;    
            }                   
        }
    }
    public void agregarFila(){
        DefaultTableModel temp = (DefaultTableModel) contenedorPaciente.getModel();
        Object nuevo[]= {"","","","","","","","","","",false};
        temp.addRow(nuevo);
    }
    private void filtrarTabla(java.util.List<service.Paciente> pacientes) {
        this.limpiarTabla();
        int i = 0;
        for (service.Paciente p : pacientes) {
            if (p.isVisible()) {
                agregarFila();                
                contenedorPaciente.setValueAt(p.getNombres()+ " " + p.getApellidoPaterno() + " " + p.getApellidoMaterno() , i, 0);                
                contenedorPaciente.setValueAt(p.getRut(), i, 1);
                i++;
            }
        }
    }  

    private static java.util.List<service.Paciente> filtrarPacientes(java.lang.String buscar) {
        service.AgendaWS_Service service = new service.AgendaWS_Service();
        service.AgendaWS port = service.getAgendaWSPort();
        return port.filtrarPacientes(buscar);
    }

    private static java.util.List<service.Paciente> listarPacientes() {
        service.PacienteWS_Service service = new service.PacienteWS_Service();
        service.PacienteWS port = service.getPacienteWSPort();
        return port.listarPacientes();
    }
}
